package com.otaviolube.jokenpo;

import java.util.Random;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class JokenpoActivity extends ActionBarActivity {

	private Random numeroAleatorio;
	
	Button pedra, papel, tesoura;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_jokenpo);
		
		/*
		//Start Fragment
		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
		*/
		
		//Starting code of project
		numeroAleatorio = new Random();
		
		
		//Botao da pedra
		Button pedra = (Button) findViewById(R.id.button1);
		pedra.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				findViewById(R.id.imageView1).setBackgroundResource(R.drawable.pedra);
				realizarJogadaPC(1);
			}
		});
		
		
		//Botao do papel
		Button papel = (Button) findViewById(R.id.button2);
		papel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				findViewById(R.id.imageView1).setBackgroundResource(R.drawable.papel);
				realizarJogadaPC(2);
			}
		});
		
		//Botao da tesoura
		Button tesoura = (Button) findViewById(R.id.button3);
		tesoura.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				findViewById(R.id.imageView1).setBackgroundResource(R.drawable.tesoura);
				realizarJogadaPC(3);
			}
		});
	}
	
	//Realizar jogada
	public void realizarJogadaPC(int jogadaPlayer){
		
		int jogadaPC;
		
		jogadaPC = numeroAleatorio.nextInt(3)+1;
		
		if(jogadaPC == 1){
			findViewById(R.id.imageView3).setBackgroundResource(R.drawable.pedra);
			if(jogadaPlayer == 1){
				findViewById(R.id.imageView2).setBackgroundResource(R.drawable.empatou);
			} else if(jogadaPlayer == 2){
				findViewById(R.id.imageView2).setBackgroundResource(R.drawable.venceu);
			} else{
				findViewById(R.id.imageView2).setBackgroundResource(R.drawable.perdeu);
			}
		} else if(jogadaPC == 2){
			findViewById(R.id.imageView3).setBackgroundResource(R.drawable.papel);
			if(jogadaPlayer == 1){
				findViewById(R.id.imageView2).setBackgroundResource(R.drawable.perdeu);
			} else if(jogadaPlayer == 2){
				findViewById(R.id.imageView2).setBackgroundResource(R.drawable.empatou);
			} else{
				findViewById(R.id.imageView2).setBackgroundResource(R.drawable.venceu);
			}
		} else if(jogadaPC == 3){
			findViewById(R.id.imageView3).setBackgroundResource(R.drawable.tesoura);
			if(jogadaPlayer == 1){
				findViewById(R.id.imageView2).setBackgroundResource(R.drawable.venceu);
			} else if(jogadaPlayer == 2){
				findViewById(R.id.imageView2).setBackgroundResource(R.drawable.perdeu);
			} else{
				findViewById(R.id.imageView2).setBackgroundResource(R.drawable.empatou);
			}
		}
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.jokenpo, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_jokenpo,
					container, false);
			return rootView;
		}
	}

}
